<?php

declare(strict_types=1);

namespace app\shop\subscribe;

use think\Event;
use sheep\facade\Auth;
use sheep\exception\SheepException;
use sheep\model\Config;
use app\shop\model\order\Order as OrderModel;
use sheep\model\user\User;
use sheep\model\auth\Admin;
use yunwuxin\facade\Notification;

class OrderAftersale
{
    public function onShopOrderAftersaleChange($params)
    {
        $aftersale = $params['aftersale'];
        $order = $params['order'];
        $aftersaleLog = $params['aftersaleLog'];

        // 通知用户售后处理过程
        $user = User::where('id', $aftersale['user_id'])->find();
        $user && $user->notify(
            new \app\shop\notification\order\aftersale\OrderAftersaleChange([
                'aftersale' => $aftersale,
                'order' => $order,
                'aftersaleLog' => $aftersaleLog,
            ])
        );

        // 通知管理员售后变动
        $admins = Admin::select()->filter(function ($admin) {
            return $admin->hasAccess($admin, [      // 售后所有权限
                'shop.admin.order.aftersale.list',
                'shop.admin.order.aftersale.detail',
                'shop.admin.order.aftersale.completed',
                'shop.admin.order.aftersale.refuse',
                'shop.admin.order.aftersale.refund',
                'shop.admin.order.aftersale.addlog',
            ]);
        });
        if (!$admins->isEmpty()) {
            Notification::send(
                $admins,
                new \app\shop\notification\order\aftersale\OrderAdminAftersaleChange([
                    'aftersale' => $aftersale,
                    'order' => $order,
                    'aftersaleLog' => $aftersaleLog,
                ])
            );
        }
    }


    public function onShopOrderAftersaleCompleted()
    {
    }

    public function onShopOrderAftersaleRefuse()
    {
    }

}
